import Ember from 'ember';
import config from '../config/environment';
import SockJS from 'sockjs';
const {withdrawCash_Succ,withdrawCash_Fail,recharge_Succ,recharge_Fail,bindWechat_Succ,bindWechat_Fail} = Constants;

export default Ember.Service.extend({
  service_curStatus:Ember.inject.service("current-status"),
  socketRef: null,
  hasOpen: false,
  init: function() {
    // return;
    var _self = this;
    var wsAddr = config.socketHost + "/alerm";
    console.log("wsAddr:" + wsAddr)
    var socket = new SockJS(wsAddr);
    console.log("socket",socket);
    socket.onopen = function (e) {
      _self.myOpenHandler(e)
    };
    socket.onmessage = function (e) {
      _self.myMessageHandler(e)
    };
    socket.onclose = function (e) {
      _self.myCloseHandler(e)
    };
    this.set('socketRef', socket);
  },

  myOpenHandler(event) {
    console.log(`On open event has been called: ${event}`);
    this.set("hasOpen",true);
    this.socketLogin();
  },
  socketLogin: function(){
    //已经登录了，就不再登
    if(this.get("hasLogin")){
      return;
    }
    var curUser = this.get("service_curStatus").getUser();
    console.log("check socketLogin hasOpen:" + this.get("hasOpen") + " curuser:",curUser);
    //发送登录token绑定信息
    if(this.get("hasOpen")&&curUser&&curUser.get("token")){
      console.log("socketLogin do");
      var msg = {code:"01",token:curUser.get("token")};
      var message = JSON.stringify(msg);
      this.get('socketRef').send(message);
      //设置登录状态
      this.set("hasLogin",true);
    }
  },
  sendMessage(message){
    console.log('message is',message);
    this.get('socketRef').send(message);
  },
  myMessageHandler(event) {
    console.log("event.data get: ",event.data);
    var msg = JSON.parse(event.data);
    console.log("msg get",msg);
    console.log('message type',msg.type);
    //如果type是1 是站内信，则发送到消息栏
    if(msg.type ===1){
      var mainpageController = App.lookup("controller:business.mainpage");
      //提醒消息栏查询
      mainpageController.send("MessageNoticeCheck");
      console.log("MessageNoticeCheck");
    }
    //如果是提醒，则发送到提醒消息栏
    if(msg.type===2){
      var mainpageController = App.lookup("controller:business.mainpage");
      //提醒消息栏查询
      mainpageController.send("noticeCheck");
      //弹出提醒提示
      // mainpageController.send("noticePop");
      if(msg.detailType===4){
        var prepaidRecordController = App.lookup("controller:business.mainpage.prepaid-record");
        if(msg.business === recharge_Succ){//充值成功
          console.log('in rechargeSuccCheck here of noti');
          prepaidRecordController.send("rechargeSuccCheck");
        }
        if(msg.business === recharge_Fail){//充值失败
          prepaidRecordController.send("rechargeFailCheck");
        }
      }
      if(msg.detailType===5){
        var applyCashController = App.lookup("controller:business.mainpage.apply-cash");
        if(msg.business === bindWechat_Succ){//绑定成功
          applyCashController.send("bindwechatSuccCheck");
        }
        if(msg.business === bindWechat_Fail){//绑定失败
          applyCashController.send("bindwechatFailCheck");
        }
      }
      if(msg.detailType===3){
        console.log('in type 3 ');
        var mainController = App.lookup("controller:business.mainpage");
        mainController.send("updateAuthenticateStatus");
      }
    }
    //如果是电话事件，则发送到电话消息栏
    if(msg.type===3){
      var mainpageController = App.lookup("controller:business.mainpage");
      var content = JSON.parse(msg.content);
      //电话消息栏查询
      mainpageController.send("callCheck",content.callId);
      //弹出提示
      // mainpageController.send("callPop",content.callId);
      var subtaskListController = App.lookup("controller:business.mainpage.subtask-list");
      subtaskListController.send("callMess",content.callId);
    }
  },

  myCloseHandler(event) {
    console.log("On close event has been called: " ,event);
  },
});
